package com.fcscs.ecms.dao.hibernate;

import java.util.List;

import org.hibernate.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.fcscs.core.dao.ConnectionManager;
import com.fcscs.core.domain.BaseEntity.DBLoc;
import com.fcscs.ecms.dao.ECMSCommonConfigDao;
import com.fcscs.ecms.domain.CommonConfig;

/**
 * @author pvttinh
 * 
 */

@SuppressWarnings("unchecked")
@Repository("commonConfigDao")
public class ECMSCommonConfigDaoHibernate implements ECMSCommonConfigDao {
	
	@Autowired
	private ConnectionManager connMgr;
	//
	private static final String find_by_element_key = 
			"FROM CommonConfig cc "
		  + "WHERE cc.id.orgId = :orgId "
		  + "AND cc.id.propertyId = :propertyId ";
	
	@Override
	public List<CommonConfig> find(int orgId, int propertyId) throws Exception {
		// TODO Auto-generated method stub
		List<CommonConfig> list = null;
		try{
			Query query = this.connMgr.getCurrentSession(DBLoc.GUEST).createQuery(
					ECMSCommonConfigDaoHibernate.find_by_element_key);
			query.setInteger("orgId", orgId);
			query.setInteger("propertyId", propertyId);
			
			list = query.list();
			if(list.isEmpty()){
				throw new Exception("COMMON CONFIG NOT FOUND");
			}
		}catch (Exception e) {
			// TODO: handle exception
			throw new Exception("CommonConfig.DAO.find() - " + e.getMessage());
		}				
		
		return list;
	}
}
